package com.fick.hibernate;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class HibernateProvideUtils {
	/**
	 * AES加密工具
	 * 
	 */
	private static byte[] keybytes = { 0x31, 0x32, 0x45, 0x12, 0x42, 0x11, 0x18, 0x11, 0x31, 0x32, 0x45, 0x12, 0x22,
			0x11, 0x18, 0x11, 0x31, 0x32, 0x45, 0x12, 0x42, 0x44, 0x18, 0x11, 0x31, 0x32, 0x45, 0x12, 0x42, 0x11, 0x18, 0x19 };

	private final static String DES = "DES";

	/**
	 * 解密
	 * 
	 * @param src
	 *          数据源
	 * @param key
	 *          密钥，长度必须是8的倍数
	 * @return 返回解密后的原始数据
	 * @throws Exception
	 */
	public static byte[] decrypt(byte[] src) throws Exception {
		// DES算法要求有一个可信任的随机数源
		SecureRandom sr = new SecureRandom();
		// 从原始密匙数据创建一个DESKeySpec对象
		DESKeySpec dks = new DESKeySpec(keybytes);
		// 创建一个密匙工厂，然后用它把DESKeySpec对象转换成
		// 一个SecretKey对象
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
		SecretKey securekey = keyFactory.generateSecret(dks);
		// Cipher对象实际完成解密操作
		Cipher cipher = Cipher.getInstance(DES);
		// 用密匙初始化Cipher对象
		cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
		// 现在，获取数据并解密
		// 正式执行解密操作
		return cipher.doFinal(src);
	}

	/**
	 * 解密
	 * 
	 * @param value
	 * @return
	 */
	public static String decrypt(String value) {
		try {
			return new String(decrypt(Hex.decodeHex(value.toCharArray())));
		} catch (DecoderException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		/*
		 * String s = null; int mode = Cipher.DECRYPT_MODE; try { Cipher cipher =
		 * initCipher(mode); byte[] outBytes =
		 * cipher.doFinal(Hex.decodeHex(value.toCharArray())); s = new
		 * String(outBytes); } catch (Exception e) { e.printStackTrace(); } return
		 * s;
		 */
	}

	/**
	 * 加密
	 * 
	 * @param src
	 *          数据源
	 * @param key
	 *          密钥，长度必须是8的倍数
	 * @return 返回加密后的数据
	 * @throws Exception
	 */
	public static byte[] encrypt(byte[] src) throws Exception {
		// DES算法要求有一个可信任的随机数源
		SecureRandom sr = new SecureRandom();
		// 从原始密匙数据创建DESKeySpec对象
		DESKeySpec dks = new DESKeySpec(keybytes);
		// 创建一个密匙工厂，然后用它把DESKeySpec转换成
		// 一个SecretKey对象
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
		SecretKey securekey = keyFactory.generateSecret(dks);
		// Cipher对象实际完成加密操作.
		Cipher cipher = Cipher.getInstance(DES);
		// 用密匙初始化Cipher对象
		cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
		// 现在，获取数据并加密
		// 正式执行加密操作
		return cipher.doFinal(src);
	}

	/**
	 * 加密
	 * 
	 * @param value
	 * @return
	 */
	public static String encrypt(String value) {
		String s = null;
		try {
			byte[] outBytes = encrypt(value.getBytes());
			s = String.valueOf(Hex.encodeHex(outBytes));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return s;
	}

	public static void main(String[] args) throws Exception {
		String e1 = encrypt("adddfafa");
		System.out.println(e1);
		String e2 = decrypt(e1);
		System.out.println(e2);
	}
}
